<?php
/**
 * Game_Event_Unit
 *
 * Класс Game_Event_Unit отвечает за события создания юнитов
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Game_Event_Unit extends Game_Event_Item {

	protected static $_eventType = 'unit';
	
	/**
	 * (non-PHPdoc)
	 * @see Game_Event_Item::finish()
	 */
	public function finish($isAutoDeleteEvent = true) {
		$objEventInfo = $this->_objEvent;
		$newAmount = $objEventInfo->getField('amount') - 1;
		if ($newAmount) {
			$timeDiff = Service_Date::timeDiff($objEventInfo->getField('event_time'), $objEventInfo->getField('event_time_additional_end'));
			$oneUnitTime = $timeDiff / $newAmount;
			$newEventTime = Service_Date::timeWithDelta($objEventInfo->getField('event_time'), $oneUnitTime);
			
			$objGameEvent = new Game_Event(array(
					'ID_place' => $objEventInfo->getField('ID_place'),
					'ID_user' => $objEventInfo->getField('ID_user'),
					'event_type' => $objEventInfo->getField('event_type'),
					'ID_essence' => $objEventInfo->getField('ID_essence'),
					'amount' => $newAmount,
					'event_time' => $newEventTime,
					'event_time_additional_end' => $objEventInfo->getField('event_time_additional_end')
			));
			$objGameEvent->save();
		}
		$stmt = "
			INSERT 
			INTO `place_unit_amount`
			(
				`ID_place`, 
				`ID_place_owner`, 
				`ID_unit`,
				`amount`
			)
			VALUES(
				" . $objEventInfo->getField('ID_place') . ",
				" . $objEventInfo->getField('ID_place') . ",
				" . $objEventInfo->getField('ID_essence') . ",
				1
			)
			ON DUPLICATE KEY UPDATE `amount` = `amount` + 1;
		";
		$objDB = Service_Database::instance();
		$objDB->query($stmt);
		
		// Обновление данных о месте
		$objPlace = new Game_Place($objEventInfo->getField('ID_place'));
		$objPlace->calculateAndUpdateDynamicConsumption();
		//_calculateProduction
		//
		//saveResourcesData();
		
		if ($isAutoDeleteEvent) $this->_deleteEvent();
	}

	
}
?>